package com.caul.demo.hadoop.mapreduce.ext;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 自定义mapreduce的输入输出参数
 * Created by sdliang on 2018/3/31.
 */
public class CustomWritable implements Writable {

  /**
   * 上行流量
   */
  private int upFlow;
  /**
   * 下行流量
   */
  private int downFlow;

  /**
   * 序列化输出
   * 在MapTask阶段,collect聚合时调用: 输出结果一样时会聚合
   *
   * @param out
   * @throws IOException
   */
  public void write(DataOutput out) throws IOException {
    out.write(getUpFlow());
    out.write(getDownFlow());
  }

  /**
   * 反序列化输入(与write的顺序对应)
   *
   * @param in
   * @throws IOException
   */
  public void readFields(DataInput in) throws IOException {
    setUpFlow(in.readInt());
    setDownFlow(in.readInt());
  }

  /**
   * 为了最终的内容输出
   *
   * @return
   */
  @Override
  public String toString() {
    return "上行流量:" + getUpFlow() + "; 下行流量:" + getDownFlow();
  }

  public int getUpFlow() {
    return upFlow;
  }

  public void setUpFlow(int upFlow) {
    this.upFlow = upFlow;
  }

  public int getDownFlow() {
    return downFlow;
  }

  public void setDownFlow(int downFlow) {
    this.downFlow = downFlow;
  }
}
